/*
 * @Author: Marte
 * @Date:   2016-10-24 16:55:45
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-11-01 10:45:23
 */

'use strict';

var currApp = angular.module('index.app', []);

currApp.controller('pagectrl', ['$scope', '$http', '$window',

    function($scope, $http, $window) {
        var vm = this;
        vm.typeRemark = {
            remark: {},
            type: {}
        };

        /*获取证件类型*/
        $http.get('/api/types')
            .success(function(data, status, headers, config) {
                vm.types = data;
            })
            .error(function(data, status, headers, config) {
                vm.types = null;
            });

        /*获取评语*/
        $http.get('/api/remarks')
            .success(function(data, status, headers, config) {
                vm.remarks = data;
            })
            .error(function(data, status, headers, config) {
                vm.remarks = null;
            });

        vm.isCurrType = function(typeId) {
            return vm.currTypeId == typeId;
        };

        vm.isCurrRemark = function(remarkId) {
            return vm.currRemarkId == remarkId;
        };

        /*检查类型是否可用*/
        vm.checkType = function(checkTypeId) {
            if (!vm.currRemarkId) {
                return true;
            } else {
                return vm.typeRemark.remark[vm.currRemarkId] && vm.typeRemark.remark[vm.currRemarkId].indexOf(checkTypeId) > -1;
            }
        };
        /*检查评语是否可用*/
        vm.checkRemark = function(checkRemarkId) {
            if (!vm.currTypeId) {
                return true;
            } else {
                return vm.typeRemark.type[vm.currTypeId] && vm.typeRemark.type[vm.currTypeId].indexOf(checkRemarkId) > -1;
            }
        };

        /*重新选择，清除选中*/
        $scope.reset = function() {
            vm.currTypeId = null;
            vm.currRemarkId = null;
        };

        /*选择类型*/
        $scope.chooseType = function(event) {
            var chooseTypeId = parseInt(event.target.getAttribute('data-id'));
            //是可用类型才往下执行
            if (vm.checkType(chooseTypeId)) {
                console.log(chooseTypeId);
                vm.currTypeId = chooseTypeId;
                vm.currType = event.target.getAttribute('data-name');
                /*选择类型后获取评语*/
                //如果已经有获取过则不再获取
                if (!vm.typeRemark.type[vm.currTypeId]) {
                    $http.get('/api/typeremarks/remarks?typeid=' + vm.currTypeId)
                        .success(function(data, status, headers, config) {
                            var array = [];
                            for (var i = 0; i < data.length; i++) {
                                array.push(data[i].RemarkId);
                            }
                            vm.typeRemark.type[vm.currTypeId] = array;
                        })
                        .error(function(data, status, headers, config) {
                            vm.typeRemark.type[vm.currTypeId] = null;
                        });
                }
            }
        };

        /*选择评语*/
        $scope.chooseRemark = function(event) {
            var chooseRemarkId = parseInt(event.target.getAttribute('data-id'));
            if (vm.checkRemark(chooseRemarkId)) {
                vm.currRemarkId = event.target.getAttribute('data-id');
                vm.currRemark = event.target.getAttribute('data-name');
                console.log(vm.remarks);
                /*选择评语后获取类型*/
                //如果已经有获取过则不再获取
                if (!vm.typeRemark.remark[vm.currRemarkId]) {
                    $http.get('/api/typeremarks/types?remarkid=' + vm.currRemarkId)
                        .success(function(data, status, headers, config) {
                            var array = [];
                            for (var i = 0; i < data.length; i++) {
                                array.push(data[i].TypeId);
                            }
                            vm.typeRemark.remark[vm.currRemarkId] = array;
                        })
                        .error(function(data, status, headers, config) {
                            vm.typeRemark.remark[vm.currRemarkId] = null;
                        });

                }
            }
        };

        //进入图片列表
        $scope.gotoList = function() {
            //必须同时选择了证件类型和评语才允许进入图片列表页
            if (vm.currTypeId && vm.currRemarkId) {
                $window.location.href = '/picturelist?typeid=' + vm.currTypeId + '&remarkid=' + vm.currRemarkId + '&type='+vm.currType+'&remark='+vm.currRemark;
            }
            if (!vm.currTypeId) {
                alert('请选择证件类型');
            } else if (!vm.currRemarkId) {
                alert('请选择评语');
            }
        };

        //console.log('启动成功');

    }
]);
